/**
* User: Oleksii Savchenko
* Date: 31.01.13
* Time: 16:34
*/
describe('Implemet CRUD service with Fake server', function(){
    beforeEach(function(){
        this.server = sinon.fakeServer.create();
        this.answer = {
            id: 4,
            name: "BMV",
            price: 150650,
            dimensions: {
                "length": 2.8,
                "width": 2,
                "height": 1.8
            }
        };

        this.itemService = new ItemAjaxService();
        this.callback = sinon.spy();
    });

    afterEach(function(){
        this.server.restore();
        this.callback.reset();
    });

    it('Get item by param(s)', function(){
        var query = {
            name: 'BMV',
            price: 150650
        };

        this.server.respondWith("GET", "/api/items?name=BMV&price=150650",
            [200, { "Content-Type": "application/json" },
                JSON.stringify(this.answer)]);

        this.itemService.getItemById(query, this.callback);
        this.server.respond();
        expect(this.callback).toHaveBeenCalled();
        expect(this.callback).toHaveBeenCalledWith(this.answer);
    });

    it('Get items which contains query string in their names', function(){
        this.server.respondWith("GET", "/api/items?query=BMV",
            [200, { "Content-Type": "application/json" },
                JSON.stringify(this.answer)]);

        this.itemService.queryItemsByName('BMV', this.callback);
        this.server.respond();
        expect(this.callback).toHaveBeenCalled();
        expect(this.callback).toHaveBeenCalledWith(this.answer);
    });

    it('Get all items', function(){
        var query = {};

        var answer = [
            {
                id: 'Item_2',
                name: 'test 1'
            },
            {
                id: 'Item_3',
                name: 'test 2'
            }
        ];

        this.server.respondWith("GET", "/api/items",
            [200, { "Content-Type": "application/json" },
                JSON.stringify(answer)]);

        this.itemService.getItemById(query, this.callback);
        this.server.respond();
        expect(this.callback).toHaveBeenCalled();
        expect(this.callback).toHaveBeenCalledWith(answer);
    });

    it('Create items provided in items array on the server', function(){
        var items = {"name":"Kraz", "price": 18050};

        this.server.respondWith("POST", "/api/items",
            [200, { "Content-Type": "application/json" },
                '12']);

        this.itemService.create(items, this.callback);
        this.server.respond();
        expect(this.callback).toHaveBeenCalled();
        expect(this.callback).toHaveBeenCalledWith(12);
    });

    it('Update items', function(){
        var items =
            {
                data: {"name":"Kraz", "price": 10540},
                params: {id: 3}
            }
        this.server.respondWith("PUT", "/api/items?id=3",
            [200, { "Content-Type": "application/json" },
                'true']);

        this.itemService.update(items, this.callback);
        this.server.respond();
        expect(this.callback).toHaveBeenCalled();
        expect(this.callback).toBeTruthy();
    });

    it('Delete items on the server', function(){
        var items = [{id: 13}];

        this.server.respondWith("DELETE", "/api/items?id=13",
            [200, { "Content-Type": "text/json" },
                'true']);

        this.itemService.delete(items, this.callback);
        this.server.respond();
        expect(this.callback).toHaveBeenCalled();
        expect(this.callback).toBeTruthy();
    });
});